/** Batch represents a page **/

package qp.utils;

import java.util.Vector;
import java.io.Serializable;

import qp.operators.Debug;

public class Batch implements Serializable{

    int MAX_SIZE;  // Number of tuples per page
    static int PageSize;  /* Number of bytes per page**/

    Vector<Tuple> tuples; // The tuples in the page


	/** Set number of bytes per page **/
	public static void setPageSize(int size){
		PageSize=size;
	}

	/** get number of bytes per page **/
	public static int getPageSize(){
		return PageSize;
	}

	/** Number of tuples per page **/

    public Batch(int numtuple){
		MAX_SIZE=numtuple;
	tuples = new Vector(MAX_SIZE);
    }


    /** insert the record in page at next free location**/

    public void add(Tuple t){
	tuples.add(t);
    }

    public int capacity(){
	return MAX_SIZE;

    }

    //sort the Batch on index, add by wang suyun 2009.11.05
	public void sort(int index) {
		int min=0;
		int compareFlag;
		for(int i=0; i<size()-1;i++) {
			for(int j=i+1;j<size();j++) {
				compareFlag=Tuple.compareTuples(tuples.elementAt(min), tuples.elementAt(j),index);
				if(compareFlag>0) {
					min=j;
				}
			}
			if(min!=i) {
				Tuple tempTuple=tuples.remove(i);
				tuples.insertElementAt(tuples.elementAt(min-1),i);
				tuples.remove(min);
				tuples.insertElementAt(tempTuple, min);
			}
			min=i+1;
		}
	}
	
    public void clear(){
	tuples.clear();
    }

    public boolean contains(Tuple t){
	return tuples.contains(t);
    }

    public Tuple elementAt(int i){
	return (Tuple) tuples.elementAt(i);
    }

    public int indexOf(Tuple t){
	return tuples.indexOf(t);
    }

    public void insertElementAt(Tuple t, int i){
	tuples.insertElementAt(t,i);
    }


/* zhaowei test 2009.10.17


        public boolean isEmpty(){
	if ( tuples.size() == 0)
		return true;
	else
		return false;
    }
*/

    public boolean isEmpty(){
	return tuples.isEmpty();
    }

    public void remove(int i){
	tuples.remove(i);
    }

//zhaowei 2009.10.16
//Removes the element at the specified position in this Vector. 
//shifts any subsequent elements to the left (subtracts one from their indices). 
//Returns the element that was removed from the Vector. 
    public Tuple remove(){
	return (Tuple) tuples.remove(0);
    }
    public Tuple remove_z(int i){
	return (Tuple)  tuples.remove(i);
    }

    
    public void clean()
    	{
    		/*Removes all components from this vector and sets its size to zero.
    		This method is identical in functionality to the clear method (which is part of the List interface). */
    		tuples.removeAllElements(); 
    	}
	
    public void setElementAt(Tuple t, int i){
	tuples.setElementAt(t,i);
    }

    public int size(){
	return tuples.size();
    }

    public boolean isFull(){
	if(size() == capacity())
	    return true;
	else
	    return false;
    }
}













